package com.eet.jpa.service.impl;

import com.eet.jpa.entity.member.User;
import com.eet.jpa.repository.UserRepository;
import com.eet.jpa.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户服务实现类
 *
 * @author Administrator
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;

    @Override
    public User findById(int id) {
        return userRepository.getById(id);
    }

    @Override
    public boolean saveUser(User user) {
        userRepository.save(user);
        return true;
    }

    @Override
    public Page<User> page(int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by(Sort.Direction.DESC, "id"));
        return userRepository.findAll(pageable);
    }

    @Override
    public void saveUsers(List<User> users) {
        userRepository.saveAll(users);
    }

    @Override
    public List<User> findByEmail(String email) {
        return userRepository.findByEmail(email);
    }

    @Override
    public List<User> findByEmailAndPhone(String email, String phone) {
        return userRepository.findByPhoneAndEmail(phone, email);
    }

    @Override
    public List<Map<String, Object>> findByRole(String roleName) {
        return userRepository.findByRole(roleName);
    }

    @Override
    public List<User> findByExample(Example<User> example) {
        return userRepository.findAll(example);
    }
}
